package hua.mtreader.base;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import hua.mtreader.R;
import hua.mtreader.common.utils.ToastAlone;
import hua.mtreader.common.utils.ToolsPreferences;

/**
 * Created by Administrator on 2017/3/15.
 */

public class MoTieApplication extends Application {

    private static Context context;
    private static int mainThreadId;
    private static Thread mainThread;
    private static Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        //Context
        context = getApplicationContext();
        //mainThreadId
        mainThreadId = android.os.Process.myTid();
        //Thread-->object
        mainThread = Thread.currentThread();
        //Handler
        handler = new Handler();

        ToastAlone.getInstance().init(context);
        ToolsPreferences.init(context);

        initImageLoaderConfig();
    }

    private void initImageLoaderConfig() {
        DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.mt_def_bg)			// 显示默认图片
                .bitmapConfig(Bitmap.Config.RGB_565).cacheInMemory().cacheOnDisc().build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .defaultDisplayImageOptions(imageOptions)
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .enableLogging()
                .memoryCacheSize(2 * 1024 * 1024)
                .memoryCache(new WeakMemoryCache()).build();
        ImageLoader.getInstance().init(config);
    }

    public static Context getContext() {
        return context;
    }
    public static int getMainThreadId() {
        return mainThreadId;
    }
    public static Thread getMainThread() {
        return mainThread;
    }
    public static Handler getHandler() {
        return handler;
    }
}
